Crate print_bytes

source ·
Expand description

This crate allows printing broken UTF-8 bytes to an output stream as losslessly as possible.

Usually, paths are printed by calling Path::display or Path::to_string_lossy beforehand. However, both of these methods are always lossy; they misrepresent some valid paths in output. The same is true when using String::from_utf8_lossy to print any other UTF-8–like byte sequence.

Instead, this crate only performs a lossy conversion when the output device is known to require Unicode, to make output as accurate as possible. When necessary, any character sequence that cannot be represented will be replaced with REPLACEMENT_CHARACTER. That convention is shared with the standard library, which uses the same character for its lossy conversion functions.

Note: Windows Compatibility

OsStr and related structs may be printed lossily on Windows. Paths are not represented using bytes on that platform, so it may be confusing to display them in that manner. Plus, the encoding most often used to account for the difference is not permitted to be written to files, so it would not make sense for this crate to use it.

Windows Console can display these paths, so this crate will output them losslessly when writing to that terminal.

Features

These features are optional and can be enabled or disabled in a “Cargo.toml” file. Nightly features are unstable, since they rely on unstable Rust features.

Nightly Features

  • specialization - Provides an implementation of WriteLossy for all types.

Examples

use std::env;

use print_bytes::println_lossy;

print!("exe: ");
println_lossy(&env::current_exe()?);
println!();

println!("args:");
for arg in env::args_os().skip(1) {
    println_lossy(&arg);
}

Structs

Traits

Functions

  • Prints a value to the standard error stream.
  • Prints a value to the standard error stream, followed by a newline.
  • Prints a value to the standard output stream.
  • Prints a value to the standard output stream, followed by a newline.
  • Writes a value to a “writer”.